package com.dataseek.iot.device.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
@Data
public class OssConfig {

    @Value("${oss.endPoint}")
    private String endPoint;

    @Value("${oss.accessKeyId}")
    private String accessKeyId;

    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${oss.bucketName}")
    private String bucketName;

    @Value("${oss.prefix}")
    private String prefix;

    @Value("${oss.localPath}")
    private String localPath;

    private String SUFFER_URL="http://" + bucketName + "."+endPoint;


    /**
     * 创建仓库
     */
    public OSSClient createBucket(){
        OSSClient ossClient=new OSSClient(endPoint, accessKeyId,accessKeySecret);
        //判断是否存在仓库
        if (ossClient.doesBucketExist(bucketName)){
            System.out.println("存在仓库");
            ossClient.shutdown();
        }else{
            System.out.println("不存在仓库....创建一个仓库"+bucketName);

            CreateBucketRequest bucketRequest=new CreateBucketRequest(null);
            bucketRequest.setBucketName(bucketName);//设置仓库名称
            bucketRequest.setCannedACL(CannedAccessControlList.PublicRead);//设置仓库权限 公共读

            ossClient.createBucket(bucketRequest);
            ossClient.shutdown();
        }
        return ossClient;
    }


//    public OSSClient getOSSClient(){
//        return new OSSClient(endPoint, accessKeyId, accessKeySecret);
//    }
}
